package com.moleq.service;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.moleq.db.DatabaseHelper;
import com.moleq.db.DatabaseSchema;
import com.moleq.models.ImportData;
import com.moleq.models.InventorySetting;

public class InventorySettingService {
	Context mContext;
	private DatabaseHelper DBHelper;
	private SQLiteDatabase db;
	private static final int PageSize = 25;

	public InventorySettingService(Context context) {
		this.DBHelper = new DatabaseHelper(context);
		this.mContext = context;
	}

	public InventorySettingService(String shareDBPath) {
		this.db = SQLiteDatabase.openOrCreateDatabase(shareDBPath, null);
	}

	public InventorySetting FindData() {
		String sql = "select * from "
				+ DatabaseSchema.InventorySetting.SCHEMA_NAME;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		InventorySetting mInventorySetting = null;
		Cursor cursor = db.rawQuery(sql, null);
		if (cursor == null) {
			return null;
		}
		if (cursor.moveToNext()) {
			mInventorySetting = new InventorySetting();
			mInventorySetting.setIs_id(cursor
					.getColumnIndex(DatabaseSchema.InventorySetting.IS_ID));
			mInventorySetting.setIs_onoff(cursor.getString(cursor
					.getColumnIndex(DatabaseSchema.InventorySetting.IS_ONOFF)));
			mInventorySetting
					.setIs_server_ip(cursor.getString(cursor
							.getColumnIndex(DatabaseSchema.InventorySetting.IS_SERVER_IP)));
			cursor.close();
			return mInventorySetting;
		}
		cursor.close();
		return mInventorySetting;
	}

	public void Insert(InventorySetting object) {
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL("INSERT INTO " + DatabaseSchema.InventorySetting.SCHEMA_NAME
				+ "(" + DatabaseSchema.InventorySetting.IS_ID + ","
				+ DatabaseSchema.InventorySetting.IS_ONOFF + ","
				+ DatabaseSchema.InventorySetting.IS_SERVER_IP + ")"
				+ " VALUES(?,?,?)", new Object[] { null, object.getIs_onoff(),
				object.getIs_server_ip() });
	}

	public int Update(InventorySetting object) {
		String sql = "update " + DatabaseSchema.InventorySetting.SCHEMA_NAME
				+ " set " + DatabaseSchema.InventorySetting.IS_ONOFF + " = '"
				+ object.getIs_onoff() + "',"
				+ DatabaseSchema.InventorySetting.IS_SERVER_IP + " = '"
				+ object.getIs_server_ip() + "' where 1 = 1 ";
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL(sql);

		return 1;
	}

	public boolean Delete() {
		try {
			db = DBHelper.getWritableDatabase();
			db.execSQL("Delete from "
					+ DatabaseSchema.InventorySetting.SCHEMA_NAME);
		} catch (Exception e) {
			// TODO: handle exception
			return false;
		}
		return true;
	}
}
